package top.caoayu.base.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.caoayu.base.entity.dto.ArticleCreateDTO;
import top.caoayu.base.entity.dto.ArticleQueryDTO;
import top.caoayu.base.entity.dto.ArticleUpdateDTO;
import top.caoayu.base.entity.vo.AritcleVo;
import top.caoayu.base.service.IArticleService;
import top.caoayu.common.response.Result;
import top.caoayu.common.validetor.InsertGroup;
import top.caoayu.common.validetor.UpdateGroup;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.nio.charset.Charset;
import java.util.List;

@RestController
@Api(tags = "文章")
@RequestMapping("/articles")
public class ArticleController {
    @Resource
    private IArticleService articleServiceImpl;

    @ApiOperation(value = "文章列表")
    @PostMapping("/")
    public Result<List<AritcleVo>> list(@RequestBody ArticleQueryDTO articleDTO) {
        return Result.success(articleServiceImpl.page(articleDTO).getRecords());
    }

    @ApiOperation(value = "新增文章")
    @PostMapping("/add")
    public Result add(@Validated(InsertGroup.class) @RequestBody ArticleCreateDTO articleDTO) {
        System.out.println(Charset.defaultCharset().name());
        return Result.success(articleDTO.getContent());
    }

    @ApiOperation("文章详情")
    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable ArticleQueryDTO articleQueryDTO) {
        return Result.success(articleServiceImpl.getDetail(articleQueryDTO));
    }

    @ApiOperation(value = "编辑文章")
    @PostMapping("edit")
    public Result edit(@Validated() @RequestBody ArticleUpdateDTO article) {
        return Result.success(articleServiceImpl.saveOrUpdate(article));
    }

    @ApiOperation(value = "删除文章")
    @PostMapping("/delete")
    public Result delete(@Valid @RequestBody Long id) {
        return Result.success(articleServiceImpl.removeById(id));
    }

    @ApiOperation("真的删除")
    @PostMapping("/reallyDelete/{id}")
    public Result reallyDelete(@PathVariable Long id) {
        return Result.success(articleServiceImpl.reallyDelete(id));
    }

    @ApiOperation("真的清空")
    @PostMapping("/reallyDeleteAll")
    public Result reallyDeleteAll() {
        articleServiceImpl.reallyDeleteAll();
        return Result.success("清空成功");
    }
}
